package com.company;

import com.company.InvalidWorkHourException;

// 普通员工：按小时计薪
public class RegularEmployee extends Employee {
    private double hourlyRate; // 时薪（元/小时）
    private double workHours;  // 当月工作小时数

    // 构造方法
    public RegularEmployee(String name, String empId, double hourlyRate, double workHours) {
        super(name, empId);
        this.hourlyRate = hourlyRate;
        this.workHours = workHours;
    }

    // 计算薪资：时薪 × 工作小时数（需校验工时）
    @Override
    public double calculateSalary() throws InvalidWorkHourException {
        // 校验工时合法性（每月最大24×28=672小时）
        if (workHours < 0 || workHours > 672) {
            throw new InvalidWorkHourException();
        }
        return hourlyRate * workHours;
    }

    // 生成工资条字符串
    @Override
    public String generatePaySlip() {
        StringBuilder slip = new StringBuilder();
        slip.append("===== 工资条 =====\n")
                .append("员工类型：普通员工\n")
                .append("工号：").append(getEmpId()).append("\n")
                .append("姓名：").append(getName()).append("\n")
                .append("时薪：").append(hourlyRate).append("元/小时\n")
                .append("当月工时：").append(workHours).append("小时\n");
        try {
            slip.append("应发薪资：").append(String.format("%.2f", calculateSalary())).append("元\n");
        } catch (InvalidWorkHourException e) {
            slip.append("薪资异常：").append(e.getMessage()).append("\n");
        }
        slip.append("==================");
        return slip.toString();
    }

    // setter方法（用于更新工时）
    public void setWorkHours(double workHours) {
        this.workHours = workHours;
    }
}